home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 December / Australian PC User - December 2003 (CD2).iso / software / apps / files / dwmx2k4.exe / Disk1 / data1.cab / Configuration_En / Debugger / MM_Debug.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  7.2 KB  |  275 lines

  1.  
  2. // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  3.  
  4.  
  5. //*************************GLOBALS**************************
  6.  
  7.  
  8. var MM_bD = true;
  9. var MM_D = new String;
  10. var MM_bEval = false;
  11. var MM_bInEval = false;
  12. var MM_Depth = 0;
  13.  
  14. var MM_debugNull = '[null]';
  15. var MM_debugUndefined = '[undefined]';
  16. var MM_debugObject = '[object]';
  17. var MM_debugError = '[error]';
  18.  
  19. var MM_isIE4 = (navigator.appName != "Netscape" && 0 > navigator.appVersion.indexOf("5."));
  20.  
  21. var MM_debugBlockPropNames = new Array(
  22.     "MM_bD", 
  23.     "MM_D", 
  24.     "MM_bEval", 
  25.     "MM_bInEval", 
  26.     "MM_Depth", 
  27.     "MM_debugNull", 
  28.     "MM_debugUndefined", 
  29.     "MM_debugObject", 
  30.     "MM_debugError", 
  31.     "MM_isIE4", 
  32.     "MM_debugBlockPropNames", 
  33.     "MM_Debug", 
  34.     "MM_escQuotes", 
  35.     "MM_isDebuggerGlobal", 
  36.     "MM_getProps", 
  37.     "MM_getTypeOf", 
  38.     "MM_hasProps", 
  39.     "MM_hasPropsIE", 
  40.     "MM_printVal");
  41.     
  42.  
  43. //*************************LOCAL FUNCTIONS**************************
  44.  
  45.  
  46. function MM_Debug(val, srcId, lineNum, pos)
  47. {
  48.     // note: we won't get here if the user is evaluating something 
  49.     // that happens to call an instrumented function
  50.     // assertion: if MM_bEval is not true
  51.     if ( MM_bInEval ) {
  52.         //alert("assert failed: MM_bInEval should always be false when calling MM_Debug");
  53.         return false;
  54.     }
  55.  
  56.     var cmd = new String();
  57.     if ( MM_bEval ) {
  58.         cmd = "returnval<MM_DELIM>" + MM_printVal(val);
  59.     }
  60.     else {
  61.         cmd += "notify<MM_DELIM>";
  62.         cmd += srcId;
  63.         cmd += "<MM_DELIM>";
  64.         cmd += MM_Depth;
  65.         cmd += "<MM_DELIM>";
  66.         cmd += lineNum;
  67.         cmd += "<MM_DELIM>";
  68.         cmd += pos;
  69.     }
  70.     //if ( lineNum == 4 || lineNum == 5)
  71.     //    alert(cmd);
  72.     //var evalStatement = new String;
  73.     //do {
  74.         var retval = MM_sendDbg(cmd);
  75.         //alert(retval);
  76.         var delLen = 10;
  77.         var retcmd = new String;
  78.         var n = retval.indexOf("<MM_DELIM>");
  79.         if ( n > 0 )
  80.             retcmd = retval.substring(0,n);
  81.         else
  82.             retcmd = retval;
  83.  
  84.         MM_bEval = true;
  85.         if ( retcmd == "eval" ) {
  86.             if ( navigator.appName.indexOf("Netscape") >= 0 )
  87.                 MM_D = new String(retval.substring(n+delLen));
  88.             else
  89.                 MM_D = "eval(\""+MM_escQuotes(retval.substring(n+delLen))+"\")";
  90.         }
  91.         else if ( retcmd == "getproperties" ) {
  92.             MM_D = 'MM_getProps('+retval.substring(n+delLen)+')';
  93.         }
  94.         else if ( retcmd == "getstackdepth" ) {
  95.             MM_D = '' +
  96.             'var i = 1;\n' +
  97.             'var c = arguments.caller.callee;\n' +
  98.             'while ( c && c.arguments.caller && c.arguments.caller.callee != c ) {\n' +
  99.             '    i++;\n' +
  100.             '    c = c.arguments.caller.callee;\n' +
  101.             '}\n' +
  102.             'i;'
  103.         }
  104.         else if ( retcmd == 'continue' ) {
  105.             /*
  106.             evalStatement = "";
  107.             */
  108.             MM_D = "";
  109.             MM_bEval = false;
  110.         }
  111.         else {
  112.             //alert("Debug connection closed by Dreamweaver");
  113.             MM_D = "";
  114.             MM_bEval = false;
  115.         }
  116.     //} while ( evalStatement.length > 0 );
  117.  
  118.     // assertion: if MM_bEval is true, then MM_D.length must be greater than 0
  119.     //if ( MM_bEval && MM_D.length == 0 )
  120.     //    alert("assert failed: if MM_bEval is true, then MM_D.length should be greater than 0");
  121.  
  122.     return ( MM_D.length > 0 );
  123. }
  124.  
  125.  
  126. function MM_escQuotes(str)
  127. {
  128.     var ret = "";
  129.     // convert '\' (backslash) characters to '\\' (escaped backslash) 
  130.     temp = str;
  131.     var n = temp.indexOf("\\");
  132.     while ( n >= 0 ) {
  133.         ret += temp.substring(0, n);
  134.         ret += "\\";
  135.         temp = temp.substring(n+1,temp.length);
  136.         n = temp.indexOf("\\");
  137.     }
  138.     ret += temp;
  139.  
  140.     // convert '"' (double-quote) to '\"' (escaped double-quote)
  141.     temp = ret;
  142.     ret = "";
  143.     var n = temp.indexOf("\"");
  144.     while ( n >= 0 ) {
  145.         ret += temp.substring(0, n);
  146.         ret += "\\\"";
  147.         temp = temp.substring(n+1,temp.length);
  148.         n = temp.indexOf("\"");
  149.     }
  150.     ret += temp;
  151.  
  152.     return ret;
  153. }
  154.  
  155. function MM_isDebuggerGlobal(propName)
  156. {
  157.     var i;
  158.     var str = ""; str += propName;
  159.     for (i = 0; i < MM_debugBlockPropNames.length; i++)
  160.     {
  161.         if ( str.indexOf(MM_debugBlockPropNames[i]) >= 0 )
  162.             return true;
  163.     }
  164.     return false;
  165. }
  166.  
  167.  
  168. function MM_getProps(obj)
  169. {
  170.     var str = new String();
  171.     for ( p in obj ) {
  172.                 
  173.         // skip any props which are functions or globals declared in this file
  174.         // (in case we are looking at the props for the window object, these seem 
  175.         // to slow it down immensely and besides the delimiter <MM_DELIM> is 
  176.         // actually in the prop values)
  177.         if ( MM_isDebuggerGlobal(p) )
  178.             continue;
  179.  
  180.         str += p + "<MM_DELIM>";
  181.     
  182.         // on IE MM_printVal can cause an exception, so the IE version 
  183.         // just calls MM_printVal with an exception handler. The function 
  184.         // must be in a separate file (MM_DebugIE.js) because there is no 
  185.         // exception handling on Netscape.
  186.         var propStr;
  187.         if ( navigator.appName != "Netscape" && !MM_isIE4 ) {
  188.             propStr = MM_printValIE(obj[p]);
  189.         }
  190.         else {
  191.             propStr = MM_printVal(obj[p]);
  192.         }
  193.         str += propStr + "<MM_DELIM>";
  194.     }
  195.     return str;
  196. }
  197.  
  198. function MM_hasProps(val)
  199. {
  200.     if ( val && typeof(val) == "object" ) {
  201.         for ( p in val ) {
  202.             return true;
  203.         }
  204.     }
  205.     return false;
  206. }
  207.  
  208. function MM_printVal(val)
  209. {
  210.     var ret = "";
  211.     var valString = new String();
  212.     var hasProps = false;
  213.     if ( val != null )
  214.     {
  215.         if ( typeof(val) == "function" ) {
  216.             // don't show real value of function objects because the functions are instrumented 
  217.             // so it is too long and often useless
  218.             // Also don't check the properties because it is too slow and the properties are 
  219.             // not very useful to look at (I think there are none in IE and in Netscape the 
  220.             // properties are just the local varibles of the function which are all null)
  221.             valString = "function";
  222.         }
  223.         else {
  224.             // convert string from eval to Javascript string with += (string 
  225.             // concatenation) explicitly because Netscape does not 
  226.             // recognize the length property except on real String objects
  227.             valString += val;
  228.             //valString += val.toString(); // <--- don't do this! it crashes on IE for some objects
  229.             //alert(val +" - the length is: "+valString.length);
  230.  
  231.             // check in case it is an object with properties
  232.             // - on IE this can cause an exception, so the IE version 
  233.             // just calls MM_hasProps with an exception handler. The function 
  234.             // must be in a separate file (MM_DebugIE.js) because there is no 
  235.             // exception handling on Netscape.
  236.             if ( navigator.appName != "Netscape" && !MM_isIE4 ) {
  237.                 hasProps = MM_hasPropsIE(val);
  238.             }
  239.             else {
  240.                 hasProps = MM_hasProps(val);
  241.             }
  242.         }
  243.     }
  244.  
  245.  
  246.     // check in case it is an string that needs quotes 
  247.     // (and not the result of GetProperties and not one of 
  248.     // our default value strings)
  249.     if ( typeof(val) == "string" && 
  250.             -1 == valString.indexOf("MM_DELIM") && 
  251.             -1 == valString.indexOf(MM_debugNull) && 
  252.             -1 == valString.indexOf(MM_debugUndefined) && 
  253.             -1 == valString.indexOf(MM_debugObject) && 
  254.             -1 == valString.indexOf(MM_debugError) )
  255.     {
  256.         valString = "\"" + MM_escQuotes(valString) + "\"";
  257.     }
  258.  
  259.     // sometimes in Netscape, the string representation of the object is empty, 
  260.     // so we use a default representation in this case
  261.     if ( hasProps && valString.length == 0 )
  262.         ret = MM_debugObject;
  263.     else if ( hasProps && valString.indexOf(MM_debugObject) != 0 )
  264.         ret = MM_debugObject + " " + valString;
  265.     else if ( hasProps )
  266.         ret = valString;
  267.     else if ( val != null && valString.length > 0 )
  268.         ret = valString;
  269.     else if ( val != null )
  270.         ret = MM_debugUndefined;
  271.     else // !val
  272.         ret = MM_debugNull;
  273.     return ret;
  274. }
  275.